//
//  HELOrderTableVC.m
//  NursingAssociation
//
//  Created by wsj2015 on 2017/3/8.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "HELOrderTableVC.h"
#import "HELOrderModel.h"
#import "HELOrderListCell.h"
#import "HELPayMethondConfirmVC.h"
#import "JCAlertView.h"
#import "PayHtm5VC.h"
@interface HELOrderTableVC ()
@property (nonatomic,  assign) BOOL isFirstGetData;

@property (nonatomic,  assign) NSUInteger page;
@end

@implementation HELOrderTableVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.page = 1;
    self.isFirstGetData = YES;
    // Do any additional setup after loading the view.
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64- 49);
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"HELOrderListCell" bundle:nil] forCellReuseIdentifier:@"HELOrderListCell"];
    self.tableView.separatorStyle= UITableViewCellSeparatorStyleNone;
    
    weakSelf_define;
    NoficationCenter_Obeserve(refersh, RefreshOrderlist);
    [self.tableView wsj_HeaderRefreshBlock:^{
        [weakSelf getOrderList:YES];
        
    }];
    [self.tableView wsj_FooterRefreshBlock:^{
      [weakSelf getOrderList:NO];
      
    }];

}
- (void)refersh{

    [self.tableView.header beginRefreshing];
}

- (void)dealloc{
   
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)requestDataFirst{
    if (self.isFirstGetData) {
        [self getOrderList:YES];
        self.isFirstGetData = NO;
    }
}

- (void)getOrderList:(BOOL)isRecover{
    if (isRecover) {
        self.page = 1;
    }else{
        self.page ++ ;
    }
    
    self.tableView.loading = YES;
    weakSelf_define;
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"page"] = @(self.page);
    params[@"rows"] = @(10);
    switch (self.orderType) {
        case OrderType_unCompelet:
            params[@"status"] = @"pending_payment";
            break;
        case OrderType_compeleted:
            params[@"status"] = @"already_paid";
            break;
        case OrderType_other:
            params[@"status"] = @"canceled";
            break;
            
        default:
            break;
    }
 
    [[HELNetworkManager sharedMg] POST:helOrderList parameters:params success:^(id responseObject) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
        if (isRecover) {
            [weakSelf.dataArray removeAllObjects];
        }
        if (responseObject[@"success"] && [responseObject[@"success"] integerValue]) {
            
            NSInteger totalPage = 0;
            if (![responseObject[@"data"][@"totalPage"] isKindOfClass:[NSNull class]]) {
                totalPage = [responseObject[@"data"][@"totalPage"] integerValue];
            }else{
                totalPage = 0;
            }
            
            NSInteger currentPage = [responseObject[@"data"][@"page"] integerValue];
            if (currentPage>=totalPage) {
                [weakSelf.tableView.footer noticeNoMoreData];
            }else{
                [weakSelf.tableView.footer resetNoMoreData];
            }
            
            [weakSelf.dataArray addObjectsFromArray:[HELOrderModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]]];
            [weakSelf.tableView reloadData];
        }
        
        
    } failure:^(NSString *err) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
        
    }];
    



}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  213;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HELOrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HELOrderListCell"];
 
    HELOrderModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    
    weakSelf_define;
    if (self.orderType == OrderType_unCompelet) {
        cell.cancelOrder = ^(){
            [weakSelf cancelOrder:model.id withIndex:indexPath.row];
        };
        cell.payOrder = ^(){
            //调用 h5
            PayHtm5VC *payVC = [[PayHtm5VC alloc] init];
            NSString *url ;
            //           url = [NSString stringWithFormat:@"%@/%@/%@", [HELNetworkManager sharedMg].bathURL,helPayHtml5,weakSelf.order.id];
            url = [NSString stringWithFormat:@"%@/%@/ios/%@",[HELNetworkManager sharedMg].bathURL,helGetOrderString,model.id];
            payVC.theUrl = [NSURL URLWithString:url];
            payVC.needToken = YES;
            payVC.title = @"支付界面";
            [weakSelf.navigationController pushViewController:payVC animated:YES];

        };
    }else if (self.orderType == 1){
        cell.openFp= ^(){};
        cell.applyFP = ^(){};
    }
   
   
    
    return cell;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)cancelOrder:(NSString *)orderId withIndex:(NSInteger)index{
   
    [JCAlertView showTwoButtonsWithTitle:@"提示" Message:@"是否确认取消?" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"否" Click:nil ButtonType:JCAlertViewButtonTypeMainColor ButtonTitle:@"确认取消" Click:^{
        cwqHud(self.view);
        weakSelf_define;
        [[HELNetworkManager sharedMg] GET: [NSString stringWithFormat:@"%@/%@",helCancelOrder,orderId] parameters:nil success:^(id responseObject) {
            [hud hide:YES];
            
            [weakSelf getOrderList:YES];
            
        } failure:^(NSString *err) {
            [hud hide:YES];
        }];

    }];
    
    
    
    

}



@end
